#!/usr/bin/env bash
# Copyright (c) 2021-2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function run_build_sdk() {
    SDK_VERSION=$(grep '"version":' "$SCRIPT_DIR"/package.json | sed 's|.*"version":.*"\(.*\)".*|\1|')
    source "$SCRIPT_DIR"/build_sdk.sh build-sdk "$@"
    BUILD_DIR=${BUILD_DIR:-"$SCRIPT_DIR"}
    SDK_BUILD_ROOT="$BUILD_DIR/build-sdk"
}

function prepare_file_test2() {
    echo "build-sdk/sdk/" > "$EXPECTED"
    echo "build-sdk/sdk/package.json" >> "$EXPECTED"

    if [ "$OHOS_ARM32" = "true" ]; then
        cat "$SCRIPT_DIR"/test/test_file_expected_list_ohos_arm32.txt >> "$EXPECTED"
    fi
    if [ "$OHOS_ARM64" = "true" ]; then
	if [ "${PANDA_LLVM_BACKEND}" == "ON" ]; then
            cat "$SCRIPT_DIR"/test/test_file_expected_list_ohos_arm64.txt >> "$EXPECTED"
        else
	    cat "$SCRIPT_DIR"/test/test_file_expected_list_ohos_arm64_withoutLLVM.txt >> "$EXPECTED"
	fi
    fi
    if [ "$LINUX_ARM64_TOOLS" = "true" ]; then
        cat "$SCRIPT_DIR"/test/test_file_expected_list_arm64.txt >> "$EXPECTED"
    fi
    if [ "$LINUX_TOOLS" = "true" ]; then
	if [ "${PANDA_LLVM_BACKEND}" == "ON" ]; then
            cat "$SCRIPT_DIR"/test/test_file_expected_list_linux.txt >> "$EXPECTED"
	else
	    cat "$SCRIPT_DIR"/test/test_file_expected_list_linux_withoutLLVM.txt >> "$EXPECTED"
	fi
    fi
    if [ "$WINDOWS_TOOLS" = "true" ]; then
        cat "$SCRIPT_DIR"/test/test_file_expected_list_windows.txt >> "$EXPECTED"
    fi
    if [ "$TS_LINTER" = "true" ]; then
        cat "$SCRIPT_DIR"/test/test_file_expected_list_ts_linter.txt >> "$EXPECTED"
    fi
    if [ "$ETS_STD_LIB" = "true" ]; then
        cat "$SCRIPT_DIR"/test/test_file_expected_list_ets.txt >> "$EXPECTED"
    fi
    if [ "$ICU_DAT_FILE" = "true" ]; then
        cat "$SCRIPT_DIR"/test/test_file_expected_list_icu.txt >> "$EXPECTED"
    fi
    if [ "$ETS_SDK" = "true" ]; then
        cat "$SCRIPT_DIR"/test/test_file_expected_list_etssdk.txt >> "$EXPECTED"
    fi

    sort --version-sort "$EXPECTED" -o "$EXPECTED"
}

set -eo pipefail
set -x

export SCRIPT_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"

source "$SCRIPT_DIR"/build_sdk_lib
run_build_sdk "$@"

ACTUAL="/tmp/panda_sdk_test_actual.txt"
EXPECTED="/tmp/panda_sdk_test_expected.txt"

# Test 1
find "$SDK_BUILD_ROOT" -maxdepth 1 | sed "s|^$BUILD_DIR/||" | sort --version-sort >"$ACTUAL"

echo "build-sdk" > "$EXPECTED"
    echo "build-sdk/panda-sdk-$SDK_VERSION.tgz" >> "$EXPECTED"
echo "build-sdk/sdk" >> "$EXPECTED"
if [ "$OHOS_ARM32" = "true" ]; then
    echo "build-sdk/ohos_arm32" >> "$EXPECTED"
fi
if [ "$OHOS_ARM64" = "true" ]; then
    echo "build-sdk/ohos_arm64" >> "$EXPECTED"
fi
if [ "$LINUX_ARM64_TOOLS" = "true" ]; then
    echo "build-sdk/linux_arm64_host_tools" >> "$EXPECTED"
fi
if [ "$LINUX_TOOLS" = "true" ]; then
    echo "build-sdk/linux_host_tools" >> "$EXPECTED"
fi
if [ "$WINDOWS_TOOLS" = "true" ]; then
    echo "build-sdk/windows_host_tools" >> "$EXPECTED"
fi

sort --version-sort "$EXPECTED" -o "$EXPECTED"
cat "$ACTUAL"
cat "$EXPECTED"
diff "$ACTUAL" "$EXPECTED"

# Test 2
find "$SDK_BUILD_ROOT"/sdk/ -maxdepth 3 | sed "s|^$BUILD_DIR/||" | sort --version-sort >"$ACTUAL"

prepare_file_test2

cat "$ACTUAL"
cat "$EXPECTED"
diff "$ACTUAL" "$EXPECTED"

rm "$ACTUAL" "$EXPECTED"
